# encoding: utf-8

module Crontab
  #class SubmitReplay
    class << self

      #线上让球胜平负赛果判断
      def online_rqspf_judge(rows)
        result = ""
        rows.each do |row|
          host_score = row[:host_score].to_i
          away_score = row[:away_score].to_i

          sql = "select company_odds from football_match_rangqiu_odds where match_id='#{row[:match_id]}' and company_id=1 limit 1;"
          rqrows = CaiQiu::Core::ServiceConn.DB_WBAI[sql]
          company_odds = nil
          rqrows.each do |rqrow|
            company_odds = rqrow[:company_odds]
            break
          end
          next if company_odds.nil? #如果无让球胜平负盘口,忽略
          #puts "#{row[:match_id]},#{host_score},#{away_score},#{company_odds}"

          if host_score+company_odds > away_score
            result = "3"
          elsif host_score+company_odds == away_score
            result = "1"
          else
            result = "0"
          end
          #online_map[row[:match_id]] = result
        end
        result
      end

      #线上半全场赛果判断
      def online_half_all_judge(rows)
        result = ""
        rows.each do |row|
          host_score = row[:host_score].to_i
          away_score = row[:away_score].to_i
          next if row[:host_half_score].nil? || row[:away_half_score].nil? #如果无半全场,忽略

          host_half_score = row[:host_half_score].to_i
          away_half_score = row[:away_half_score].to_i

          if host_half_score < away_half_score && host_score < away_score
            result = "00"
          elsif host_half_score < away_half_score && host_score == away_score
            result = "01"
          elsif host_half_score < away_half_score && host_score > away_score
            result = "03"
          elsif host_half_score == away_half_score && host_score < away_score
            result = "10"
          elsif host_half_score == away_half_score && host_score == away_score
            result = "11"
          elsif host_half_score == away_half_score && host_score > away_score
            result = "13"
          elsif host_half_score > away_half_score && host_score < away_score
            result = "30"
          elsif host_half_score > away_half_score && host_score == away_score
            result = "31"
          else
            result = "33"
          end
          #online_map[row[:match_id]] = result
        end
        #puts result
        result
      end

      #线上总进球赛果判断
      def online_all_score_judge(rows)
        result = ""
        rows.each do |row|
          host_score = row[:host_score].to_i
          away_score = row[:away_score].to_i
          all_score = host_score + away_score
          #puts "#{row[:match_id]},#{host_score},#{away_score},#{company_odds}"
          if all_score == 0
            result = "0"
          elsif all_score == 1
            result = "1"
          elsif all_score == 2
            result = "2"
          elsif all_score == 3
            result = "3"
          elsif all_score == 4
            result = "4"
          elsif all_score == 5
            result = "5"
          elsif all_score == 6
            result = "6"
          else
            result = "7"
          end
        end
        #puts result
        result
      end

      #线上比分赛果判断
      def online_score_judge(rows)
        result = ""
        rows.each do |row|
          host_score = row[:host_score].to_i
          away_score = row[:away_score].to_i

          if host_score > 4 && away_score > 2 && host_score > away_score
            result = "9-0"
          elsif host_score > 2 && away_score > 4 && host_score < away_score
            result = "0-9"
          elsif host_score >= 4 && away_score >= 4 && host_score == away_score
            result = "9-9"
          else
            result = host_score.to_s + "-" +away_score.to_s
          end
          #online_map[row[:match_id]] = result
        end
        #puts result
        result
      end

      def update_reason_season_result(type,match_id,company_id,play_code,pred_result,result_matching)
        Prediction::Service::ReasonSeasonResult.update_result(type,match_id,company_id,play_code,pred_result,result_matching)
      end

    end
  #end
end